package cn.yinyu.queue.module.ecg.service.devmanage;

import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DeviceStatisticDO;
import cn.yinyu.queue.module.infra.api.config.ConfigApi;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.yinyu.queue.module.ecg.controller.admin.devmanage.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DeviceDO;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import cn.yinyu.queue.module.ecg.dal.mysql.devmanage.DeviceMapper;

import javax.annotation.Resource;

import static cn.yinyu.queue.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.yinyu.queue.module.ecg.Constants.DEV_CODE_CHOICE;
import static cn.yinyu.queue.module.ecg.Constants.ECG_SCREEN_PANE_PASSED_KEY;
import static cn.yinyu.queue.module.ecg.enums.ErrorCodeConstants.*;

/**
 * 设备 Service 实现类
 *
 * @author majianbo
 */
@Service
@Validated
public class DeviceServiceImpl implements DeviceService {
    @Resource
    private ConfigApi configApi;

    @Resource
    private DeviceMapper deviceMapper;

    @Override
    public Integer createDevice(DeviceSaveReqVO createReqVO) {
        // 读取 设备编号选择 系统配置
        Integer devCodeChoice = Integer.valueOf(configApi.getConfigValueByKey(DEV_CODE_CHOICE));
        if (0 == devCodeChoice)
            createReqVO.setDevId( createReqVO.getDevCodeIntrinsic() );
        else if (1 == devCodeChoice)
            createReqVO.setDevId( createReqVO.getDevCodeHosp() );
        else if (2 == devCodeChoice)
            createReqVO.setDevId( createReqVO.getDevCodeDept() );
        else
            createReqVO.setDevId( createReqVO.getDevCodeDept() );

        // 插入
        DeviceDO device = BeanUtils.toBean(createReqVO, DeviceDO.class);
        deviceMapper.insert(device);
        // 返回
        return device.getId();
    }

    @Override
    public void updateDevice(DeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceExists(updateReqVO.getId());

        // 读取 设备编号选择 系统配置
        Integer devCodeChoice = Integer.valueOf(configApi.getConfigValueByKey(DEV_CODE_CHOICE));
        if (0 == devCodeChoice)
            updateReqVO.setDevId( updateReqVO.getDevCodeIntrinsic() );
        else if (1 == devCodeChoice)
            updateReqVO.setDevId( updateReqVO.getDevCodeHosp() );
        else if (2 == devCodeChoice)
            updateReqVO.setDevId( updateReqVO.getDevCodeDept() );
        else
            updateReqVO.setDevId( updateReqVO.getDevCodeDept() );

        // 更新
        DeviceDO updateObj = BeanUtils.toBean(updateReqVO, DeviceDO.class);
        deviceMapper.updateById(updateObj);
    }

    @Override
    public Integer updateDeviceState(DeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceExists(updateReqVO.getId());
        // 更新
        DeviceDO updateObj = BeanUtils.toBean(updateReqVO, DeviceDO.class);
        return deviceMapper.updateDevState(updateObj.getDevId(), updateObj.getState(), updateObj.getStateDate(), updateObj.getComment());
    }

    @Override
    public void deleteDevice(Integer id) {
        // 校验存在
        validateDeviceExists(id);
        // 删除
        deviceMapper.deleteById(id);
    }

    private void validateDeviceExists(Integer id) {
        if (deviceMapper.selectById(id) == null) {
            throw exception(DEVICE_NOT_EXISTS);
        }
    }

    @Override
    public DeviceDO getDevice(Integer id) {
        return deviceMapper.selectById(id);
    }

    @Override
    public DeviceDO getDevice(String devId) {
        return deviceMapper.getDeviceByDevId(devId);
    }

    @Override
    public PageResult<DeviceDO> getDevicePage(DevicePageReqVO pageReqVO) {
        return deviceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DeviceStatisticDO> getDeviceStatistic(DevicePageReqVO pageReqVO) {
        return deviceMapper.getDevStatistic(pageReqVO);
    }

}